package cn.wolfcode.trip.base.service.impl;


import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public void registe(User user) {
        String email=userMapper.selectByEmail(user.getEmail());
        if (email!=null){
            throw new RuntimeException("该用户已存在！");
        }
        //设置默认数据
        user.setPlace("亚洲");
        user.setGender(User.GENDER_NONE);
        user.setHeadImgUrl("/upload/head.jpg");
        user.setCoverImgUrl("/upload/bg.jpeg");
        user.setSign("请为您的个性签名补上您的精美语句喔！");


        userMapper.insert(user);
    }

    @Override
    public User login(String email, String password) {
        User user = userMapper.login(email, password);
        if (user==null) {
            throw new RuntimeException("账号或密码不匹配!");
        }


        return user;
    }

    @Override
    public PageInfo<User> listForList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<User> list = userMapper.listForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateByPrimaryKey(user);
    }
}
